import Vue from 'vue'
import ElementUI from 'element-ui'
// 全局配置 仅存在一条message
let messageInstance = null;
/**
 *  
 *  仅显示一条Message
 *  适用于：
 *      1. 实例方法调用
 *      2. 四个类型直接调用的方法
 */
const resetMessage = (options) => {
  if (messageInstance) {
    messageInstance.close()
  }
  messageInstance = ElementUI.Message(options)
};
const typeArr = ['error', 'success', 'info', 'warning'];
typeArr.forEach(type => {
  resetMessage[type] = options => {
    if (typeof options === 'string') {
      options = {
        message: options
      }
    }
    options.type = type;
    return resetMessage(options);
  }
});

// export const confirmMsg = '确认执行当前操作?';
export const confirmMsg = '是否执行该操作?';
export const msgPrompt = {
  success: (message = '操作成功!') => {
    resetMessage({ type: 'success', message, showClose: true })
  },
  warning: (message = '请选择操作项!') => {
    resetMessage({ type: 'warning', message, showClose: true })
  },
  error: (message = '操作失败!') => {
    resetMessage({ type: 'error', message, showClose: true })
  },
  info: (message = '已取消!') => {
    resetMessage({ type: 'info', message, showClose: true, })
  }
}

export const globalMessage = (message = '', type = 'warning', showClose = true) => {
  resetMessage({ showClose, message, type })
}
Vue.prototype.$confirmMsg = confirmMsg // 确认对话框 Message 参数
Vue.prototype.$msgPrompt = msgPrompt // 全局Message提示框实例,调用 success warning error
Vue.prototype.$globalMessage = globalMessage


